package vn.qnamuni.calendar.services;

//import vn.qnamuni.calendar.activity.EventActivity.ResponseReceiver;
import android.app.IntentService;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.facebook.HttpMethod;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;

/*
 * 
 * Add service to manifest
 */
/**
 * @author HP
 * 
 */
public class FacebookService extends IntentService {
	private static final String LOG = FacebookService.class.getName();

	public static final String LOG_IN = "LOG_IN";
	public static final String LOG_OUT = "LOG_OUT";
	public static final String POST_WALL = "POST_WALL";
	public static final String GET_INFO = "GET_INFO";
	public static final String UPDATE = "UPDATE";

	public static boolean isActive() {
		Session session = Session.getActiveSession();
		if (null != session) {
			return true;
		}
		return false;
	}
	
	public FacebookService() {
		super("FacebookService");
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		Log.d(LOG, "Facebook service started");
//		Intent broadCastIntent = new Intent();

		String action = intent.getStringExtra("action");
		// TODO: repair this
		if (null != action && action.equals(GET_INFO)) {
			getFacebook("", intent);
		} else if (action.equals(UPDATE)) {

			// get changed info
		}

//		broadCastIntent.setAction(action);
//		this.sendBroadcast(broadCastIntent);

	}

	public void postOnWall(String msg) {
		try {

			Bundle postParams = new Bundle();
			postParams.putString("message", "My wall picture");

			Session session = Session.getActiveSession();
			if (session != null) {
				Log.v("PostWa", "not null");
				Request request = new Request(session, "me/feed", postParams,
						HttpMethod.POST);

				RequestAsyncTask task = new RequestAsyncTask(request);
				task.execute();
				Log.v("PSt", "1");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param username
	 * @param intent
	 */
	private void getFacebook(String username, Intent intent) {
		/*
		 * String jString =
		 * "{\"data\":[{\"birthday_date\":null,\"pic_square\":\"http://profile.ak.fbcdn.net/hprofile-ak-ash2/1086817_519242275_472095453_q.jpg\",\"uid\":519242275,\"name\":\"Thien Vu\"}]}"
		 * ; JSONObject jObject; try { jObject = new JSONObject(jString);
		 * ServiceHelper.getInstance(this).processResult(jObject); } catch
		 * (JSONException e) { // TODO Auto-generated catch block
		 * Log.v("Faceservice", e.getMessage()); }
		 */

		// Session session = Session.getActiveSession();

		Log.v("TAG", "Fetching");
		String fqlQuery = "SELECT uid, name, pic_square, birthday_date, username FROM user WHERE uid IN "
				+ "(SELECT uid2 FROM friend WHERE uid1 = me())";
		Bundle params = new Bundle();
		params.putString("q", fqlQuery);
		Session session = Session.getActiveSession();

		Request request = new Request(session, "/fql", params, HttpMethod.GET);
		Response response = request.executeAndWait();
//		try {
//			// Open network
//
//			
//			// processing done here�.
//			Intent broadcastIntent = new Intent();
//			broadcastIntent.setAction(ResponseReceiver.ACTION_RESP);
//			broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
//			broadcastIntent.putExtra(ResponseReceiver.PARAM_OUT_MSG, ResponseReceiver.FAILURE); 
//			sendBroadcast(broadcastIntent);
//			ServiceHelper.getInstance(this).callSync(response.getGraphObject().getInnerJSONObject());
//		} catch (Exception e) {
//			Log.v("GetInfo Faceservice", e.getMessage());
//		}
	}
	
}
